package com.example.cybz.acl.controller;

import com.example.cybz.acl.service.PermissionService;
import com.example.cybz.common.result.Result;
import com.example.cybz.model.acl.Permission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Log4j2
@RestController
@Api(tags = "用户接口")
@RequestMapping("/admin/acl/permission")
@RequiredArgsConstructor
public class PermissionController {

    private final PermissionService permissionService;


    @ApiOperation(value = "获取所有菜单")
    @GetMapping
    public Result<List<Permission>> list() {
        List<Permission> list = permissionService.queryAllPermission();
        return Result.ok(list);
    }


    @ApiOperation(value = "添加菜单")
    @PostMapping("/save")
    public Result<Object> save(@RequestBody Permission permission) {
        // 加密密码
        boolean save = permissionService.save(permission);
        return save ? Result.ok() : Result.fail();
    }

    @ApiOperation(value = "修改菜单")
    @PutMapping("/update")
    public Result<Object> update(@RequestBody Permission permission) {
        boolean updateById = permissionService.updateById(permission);
        return updateById ? Result.ok() : Result.fail();
    }

    @ApiOperation(value = "递归删除菜单")
    @DeleteMapping("/remove/{id}")
    public Result<Object> list(@PathVariable Long id) {
        boolean removeChildById = permissionService.removeChildById(id);
        return removeChildById ? Result.ok() : Result.fail();
    }

    @ApiOperation(value = "为角色分配菜单")
    @PostMapping("/toAssign")
    public Result<Object> toAssign(@RequestParam List<Long> permissionList, @RequestParam Long RoleId) {
        boolean savaAdminRole = permissionService.savaPermissionRole(RoleId, permissionList);
        return savaAdminRole ? Result.ok() : Result.fail();
    }

}
